// $codepro.audit.disable lossOfPrecisionInCast, numericLiterals

// lossOfPrecisionInCast - we simple use this to calculate out price increases and such
// with decimals that are not needed, so we simply truncate with no worries

// numericLiterals - I assigned most of the numbers to variables and replaced them in the
// code but the audit told me to replace the numeric literal in cases like 
// int x = 3; which isn't possible to do without numbers
/**
 * Import Random library
 */
import java.util.Random;

/**
 * This class stores and creates the goods.
 * 
 * @author Hung Lac
 * @version 1.0
 */
public class Goods implements java.io.Serializable{
	/**
	 * These ints relate to the goods being bought and sold
	 */
	private int price, count, sold;
	
	/**
	 * This string is the name of the goods model
	 */
	private final String name;
	
	/**
	 * Sets an array of goods and their prices.
	 *
	 * @param goodsModel - the model for the goods
	 * @param planet - the planet the market is on
	 */
	public Goods(GoodsModel goodsModel, Planet planet){
		final Random rand= new Random();
		final double decreasePrice = .85;
		final double increasePrice = 1.15;
		final int tech = 20;
		sold=0;
		name=goodsModel.name;
		price=goodsModel.basePrice + goodsModel.ipl * (planet.getTech()
				- goodsModel.mtlp) + (int) (Math.pow(-1, rand.nextInt(2)))
				* goodsModel.var;
		if(planet.getTech() >= goodsModel.mtlp){
			if(planet.getTech() == goodsModel.ttp){
				count=(int) (Math.random() * tech) + tech;
			}
			else{
				count=(int) (Math.random() * tech);
			}
		}
		if(planet.resArray[planet.getResources()].equals(goodsModel.cr)){
			price=(int) (price * decreasePrice);
		}
		if(planet.resArray[planet.getResources()].equals(goodsModel.er)){
			price=(int) (price * increasePrice);
		}
	}
	
	/**
	 * This method sets some basic statistics for the goods.
	 */
	public Goods() {
		// TODO Auto-generated constructor stub
		sold=0;
		price=0;
		count=0;
		name=null;
	}

	/**
	 * Method for getting the name
	 * 
	 * @return The name of the goods model
	 */
	public String getName(){
		return name;
	}
	
	/**
	 * Method for getting the price
	 * 
	 * @return The price of the object
	 */
	public int getPrice(){
		return price;
	}
	
	/**
	 * Method for getting the count
	 * 
	 * @return The current count
	 */
	public int getCount(){
		return count;
	}
	
	/**
	 * This method updated the count variable
	 * 
	 * @param value The amount to increase the count by
	 */
	public void setCount(Integer value) {
		// TODO Auto-generated method stub
		count+=value;
	}
	
	/**
	 * Method for getting the count as a string
	 * 
	 * @return The string version of count
	 */
	public String getCountString(){
		String res="";
		res+=count;
		return res;
	}
	
	/**
	 * Method that prints information about the good
	 * @return The information related to the object
	 */
	@Override
	public String toString(){
		String res="";
		res+=name + " " + price + " " + count;
		return res;
	}
	
	/**
	 * Method that gets whether or not the good gets sold
	 * 
	 * @return How much the good is sold for
	 */
	public int getSold() {
		// TODO Auto-generated method stub
		return sold;
	}
	
	/**
	 * Method that sets the amount good is sold for 
	 * 
	 * @param value The amount to increase sold by
	 */
	public void setSold(Integer value) {
		// TODO Auto-generated method stub
		sold += value;
	}
	
	/**
	 * Method for getting the string representation of sold
	 * 
	 * @return The string version of sold
	 */
	public String getSoldString() {
		// TODO Auto-generated method stub
		return ("" + sold);
	}
	
	/**
	 * Method that sets the price of the good
	 * 
	 * @param price The price of the good
	 */
	public void setPrice(int price) {
		// TODO Auto-generated method stub
		this.price=price;
	}
}
